let uEmailInp = document.querySelector("#username")
let uEmailText = document.querySelector(".usertext")
let passwordInp = document.querySelector("#password")
let passwordText = document.querySelector(".passtext")
let photoEl = document.querySelector(".headphoto img")
let restBtn = document.querySelector(".rest")
let overBtn = document.querySelector(".over")
//获取页面的token
let token = window.location.href.slice(window.location.href.indexOf("token=", 0) + 6)
let photo="default1.jpg"
//输入框聚焦失焦的效果
uEmailInp.addEventListener("blur", uEmailInpBlur)
uEmailInp.addEventListener("focus", uEmailInpFocus)
passwordInp.addEventListener("blur", wordInpBlur)
passwordInp.addEventListener("focus", wordInpFocus)
// 切换头像
restBtn.addEventListener("click", togglePhoto)
// 完成修改
overBtn.addEventListener("click", overUser)
function uEmailInpFocus(e) {
    let inp = e.target
    inp.placeholder = ""
    uEmailText.classList.add("inputclick")

}
function uEmailInpBlur(e) {
    let inp = e.target
    if (inp.value == "") {
        inp.placeholder = "用户名 必须在4~20个字符之间"
    }
    if (inp.value == "" && inp.placeholder == "用户名 必须在4~20个字符之间") {

        uEmailText.classList.remove("inputclick")
    }
}
function wordInpFocus(e) {
    let inp = e.target
    inp.placeholder = ""
    passwordText.classList.add("inputclick")

}

function wordInpBlur(e) {
    let inp = e.target
    if (inp.value == "") {
        inp.placeholder = "密码(8-20位)特殊字符可选(,._!@#$^&*)"
    }
    if (inp.value == "" && inp.placeholder == "密码(8-20位)特殊字符可选(,._!@#$^&*)") {
        passwordText.classList.remove("inputclick")
    }
}
//切换头像
async function togglePhoto() {
    //获取头像列表
    let res = await axios({
        method: 'get',
        url: 'http://121.37.252.168:8080/api/user/getDefaultAvatarList',
    })

    let photoList = res.data.data.avatarList
    //  获取0-7的随机数字
    let num = Math.floor(Math.random() * (photoList.length + 1))
     photo = photoList[num]

    //图片切换实现
    photoEl.src = `http://121.37.252.168:8080/api/user/getAvatar/${photo}`
}

//  完成注册
async function overUser() {
    console.log("dfa",photo);
    //判断用户名是否正确
    if (!judgeName()) {
        judgeRemoveTime()
    }
    // 判断密码是否正确
    else if (!judgePassWord()) {
        judgeRemoveTime()
    }
    else {
        let res = await axios({
            method: 'post',
            url: 'http://121.37.252.168:8080/api/user/register',
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                username: uEmailInp.value,
                token: token,
                password: passwordInp.value,
                avatar: photo,
            }
        })
        if(code=200){
            window.location.href="../public/login.html"
        }
    
    }

}

